<%@page import="com.ddd.entity.Article"%>
<%@page import="com.ddd.service.ArticleService"%>
<%@page import="com.ddd.context.Parameters"%>
<%@page import="com.ddd.util.Util"%>
<%@page import="com.ddd.entity.Journal"%>
<%@page import="com.ddd.service.JournalService"%>
<%@page import="com.ddd.entity.Contract"%>
<%@page import="com.ddd.service.ContractService"%>
<%@page import="com.ddd.entity.Publisher"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.PublisherService"%>
<%@page import="com.ddd.context.Context"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    // load data
    JournalService journalService = new JournalService(request);
    ArrayList journalList = journalService.loadAll();

    PublisherService publisherService = new PublisherService(request);
    ArrayList publisherList = publisherService.loadAll();

    // 必須最後load, 因為會將筆數寫入session
    ArticleService articleService = new ArticleService(request);
    ArrayList articleList = articleService.loadAll();
    
    //搜尋功能
    ArrayList articleList_s = new ArrayList();
    String temp4=""; //查詢選擇的欄位value
    String temp3=""; //查詢的文字
    String temp2=""; //暫時抽出查詢欄位的值以便比對
    //temp1 是將整列的資料倒入以便比對分析
    
    for (int i=0;i<articleList.size();i++) {
        Article temp1=(Article) articleList.get(i); 
        
        if(request.getParameter("search_f")!=""&&request.getParameter("search_f")!=null){
            temp4=request.getParameter("search_f");
                if(temp4.contains("a")){
                    temp2=temp1.getName();
                }
                else if(temp4.contains("b")){
                    if(temp1.getAuthorized()!=null){
                        temp2=temp1.getAuthorized();}
                    else{
                        temp2="abcdefg";}
                }
                else if(temp4.contains("c")){
                    if(temp1.getCurrJournal()!=null){
                    temp2=temp1.getCurrJournal();}
                    else{
                        temp2="abcdefg";}                    
                }
                else if(temp4.contains("d")){
                    temp2=temp1.getTjcdCode();
                }
        }
        if(request.getParameter("search_value")!=""&&request.getParameter("search_value")!=null){
            temp3=request.getParameter("search_value");
            if(temp2.contains(temp3)){
                articleList_s.add(articleList.get(i));
            }
        }else{ 
            if(temp4.contains("b")){
                temp3=request.getParameter("search_value1");
                if(temp2.contains(temp3)){
                    articleList_s.add(articleList.get(i));}
            }else if(temp4.contains("c")){
                temp3=request.getParameter("search_value2");
                if(temp2.contains(temp3)){
                    articleList_s.add(articleList.get(i));
                }
            }else{
                articleList_s.add(articleList.get(i));
            }
        }     
    }
    //搜尋End
    
    Util.checkBEAuthorized(request, response);

    String message = request.getParameter("message");
    String gotoPage = request.getParameter("gotoPage");

    Parameters para = new Parameters(request);
    if (Util.isNotBlank(gotoPage)) {
        para.setCurrentPage(Integer.parseInt(gotoPage));
    } else {
        para.setCurrentPage(1);
    }
     para.setTotalSize(articleList_s.size());
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>

        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript">
            function doSelect(){
               $('#form2').submit();
            }
            
            function doUpdate (tjcd){
                window.location = "articledetails.jsp?mode=update&tjcdCode=" + tjcd;
            }
            
            function doDelete (tjcd){
                $( "#dialog_delete" ).dialog({
                    title: "刪除文章",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "刪除": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doArticle?mode=delete&tjcdCode=" + tjcd;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
            
            function gotoPage(page){
                window.location = "article.jsp?gotoPage=" + page+"&search_value="+"<%=temp3%>"+"&search_f="+"<%=temp4%>";
            }
            
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
            function change_select_f(s) 
            { 
                if(s=='b')
                    {
                        document.getElementById('search_value').style.display = 'none';
                        document.getElementById('search_value1').style.display = 'inline';
                        document.getElementById('search_value2').style.display = 'none';
                        search_value.value="";
                    }
                if(s=='c')
                    {
                        document.getElementById('search_value').style.display = 'none';
                        document.getElementById('search_value1').style.display = 'none';
                        document.getElementById('search_value2').style.display = 'inline';
                        search_value.value="";
                    }
                if(s=='a' || s=='d')
                    {
                        document.getElementById('search_value1').style.display = 'none';
                        document.getElementById('search_value').style.display = 'inline';
                        document.getElementById('search_value2').style.display = 'none';
                        search_value.value="";
                    }
            } 
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <!-- 搜尋開始 -->
                        <div>
                            <form id="form2" name="form2" action="/DDD/BE/article.jsp" method="post">
                                搜尋：
                                <select name="search_f" id="search_f" onchange="change_select_f(this.options[this.options.selectedIndex].value)">
                                    <option value="a" <%=(temp4.contains("a"))?"selected=\"selected\"":""%>>文章篇名</option>
                                    <option value="b" <%=(temp4.contains("b"))?"selected=\"selected\"":""%>>有無授權</option>
                                    <option value="c" <%=(temp4.contains("c"))?"selected=\"selected\"":""%>>現刊/過刊</option>
                                    <option value="d" <%=(temp4.contains("d"))?"selected=\"selected\"":""%>>TJCD碼</option>
                                </select>
                                
                                <input type="text" name="search_value" id="search_value" value="<%=((temp4.contains("b"))||(temp4.contains("c")))?"":temp3%>" style="<%=((temp4.contains("b"))||(temp4.contains("c")))?"display:none":""%>"/>
                                <select style="<%=(temp4.contains("b"))?"":"display:none"%>" id="search_value1" name="search_value1">
                                    <option value="on" <%=(temp3.contains("on"))?"selected=\"selected\"":""%>>有</option>
                                    <option value="abcdefg" <%=(temp3.contains("abcdefg"))?"selected=\"selected\"":""%>>無</option>
                                </select>
                                <select style="<%=(temp4.contains("c"))?"":"display:none"%>" id="search_value2" name="search_value2">
                                    <option value="on" <%=(temp3.contains("on"))?"selected=\"selected\"":""%>>過刊</option>
                                    <option value="abcdefg" <%=(temp3.contains("abcdefg"))?"selected=\"selected\"":""%>>現刊</option>
                                </select>
                                <input type="button" value="搜尋" name="search_submit" id="search_submit" onclick="doSelect()"/>
                            </form>
                        </div>
                        <!-- 搜尋End -->
                        <!-- starrt #content -->
                        <div id="content">
                            <p>
                                <a href="articledetails.jsp?mode=add">新增文章</a>
                            </p>
                            <table>
                                <thead>
                                    <tr>
                                        <th>TJCD碼</th>
                                        <th>期刊名稱</th>
                                        <th>篇名</th>
                                        <th>出版</th>
                                        <th>卷</th>
                                        <th>期</th>
                                        <th>負責人</th>
                                        <th>最後修改者</th>
                                        <th>修改</th>
                                        <th>刪除</th>
                                    </tr>
                                </thead>
                                <%
                                    int left = para.getPageSize();
                                    for (int i = (para.getCurrentPage() - 1) * para.getPageSize(); i < para.getCurrentPage() * para.getPageSize() && i < para.getTotalSize(); i++) {
                                        left--;

                                        Article a = (Article) articleList_s.get(i);

                                        Journal j = null;
                                        for (int k = 0; k < journalList.size(); k++) {
                                            Journal tmp = (Journal) journalList.get(k);
                                            if (a.getFkJournalId().equals(tmp.getId())) {
                                                j = tmp;
                                                break;
                                            }
                                        }

                                        Publisher p = null;
                                        for (int k = 0; k < publisherList.size(); k++) {
                                            Publisher tmp = (Publisher) publisherList.get(k);
                                            if (j.getFkPublisherId().equals(tmp.getId())) {
                                                p = tmp;
                                                break;
                                            }
                                        }
                                %>
                                <tr>
                                    <td><%=a.getTjcdCode()%></td>
                                    <td><%=j.getName()%></td>
                                    <td><%=a.getName()%></td>
                                    <td><%=p.getName()%></td>
                                    <td><%=a.getVolume()%></td>
                                    <td><%=a.getIssue()%></td>
                                    <td><%=a.getCid()%></td>
                                    <%if (a.getUid() != null) {%>
                                    <td><%=a.getUid()%></td>
                                    <%} else {%>
                                    <td>&nbsp;</td>
                                    <%}%>
                                    <td><a href='javascript:doUpdate("<%=a.getTjcdCode()%>");'>修改</a></td>
                                    <td><a href='javascript:doDelete("<%=a.getTjcdCode()%>");'>刪除</a></td>
                                </tr>
                                <%}%>

                                <%for (int i = 0; i < left; i++) {%>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                </tr>
                                <%}%>
                            </table>
                            <br /> 
                            <p align="center">
                                <%=para.getCurrentPage()%> / <%=para.getTotalPage()%> (總筆數: <%=para.getTotalSize()%>)
                            </p>
                            <p align="center">
                                <%if (para.getCurrentPage() > 1) {%>
                                <input type="button" value="上一頁" onclick='gotoPage(<%=para.getCurrentPage() - 1%>)'>
                                <%}%>
                                <%if (para.getCurrentPage() < para.getTotalPage()) {%>
                                <input type="button" value="下一頁" onclick='gotoPage(<%=para.getCurrentPage() + 1%>)'>
                                <%}%>
                            </p>

                        </div>
                        <!-- end #content -->

                        <!-- end #sidebar -->
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_delete" style="display: none">
            <p>是否要刪除文章？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>

    </body>
</html>
